﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace WCFServiceWebRole1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AdminService" in code, svc and config file together.
    public static partial class Extensions
    {
        public static StoreRecord ConverttosStoreRecord(this StoreRecord user, UsersDataModel item)
        {
            user.Ip = item.IP;
            user.Password = item.Password;
            user.Port = item.Port;
            user.Status = item.Status;
            user.Username = item.Username;

            return user;

        }
        public static UsersDataModel ConverttoUsersDataModel(this UsersDataModel user, StoreRecord item)
        {
            user.IP = item.Ip;
            user.Password = item.Password;
            user.Port = item.Port;
            user.Status = item.Status;
            user.Username = item.Username;

            return user;
        }
    }
    public class AdminService : IAdminService
    {
        public CloudStorageAccount acc = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
        UsersDataSource users;

        #region IAdminService Members
        public AdminService()
        {
            users = new UsersDataSource(acc);
        }

        StoreRecord IAdminService.getUser(string name)
        {
            var result = from d in users.Select(name)
                         select new StoreRecord().ConverttosStoreRecord(d);
            return result.First();
        }

        void IAdminService.addUser(StoreRecord user)
        {
            users.Add(new UsersDataModel().ConverttoUsersDataModel(user));
        }

        void IAdminService.editUser(StoreRecord user)
        {
            users.Edit(user);
        }
        void IAdminService.deleteUser(string user)
        {
            users.Delete(user);
        }

        List<StoreRecord> IAdminService.getAllUsers()
        {
            var result = from d in users.SelectAll()
                         select new StoreRecord().ConverttosStoreRecord(d);
            return result.ToList();
        }

        #endregion
    }
}
